package com.bjtg.threegoclinet.util;

import android.content.Context;
import android.content.Intent;
import android.os.Environment;

import com.bjfl.saleManager.util.LogUtil;
import com.bjtg.threegoclinet.base.MyApplication;
import com.bjtg.threegoclinet.bean.ParameterSet;
import com.bjtg.threegoclinet.base.ParseJsonParameterSet;
import com.bjtg.threegoclinet.base.PullParser;
import com.bjtg.threegoclinet.service.AtuoDeleteLogService;
import com.bjtg.threegoclinet.service.AutoUpdateService;
import com.bjtg.threegoclinet.service.HeartBeatService;
import com.log.LatteLogger;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;


/**
 * 操作配置文件公共类
 * 日期:2017/05/25
 */

public class JpushSharedPreferences {


    private static final String TAG = "JpushSharedPreferences";


    /**
     * 初始化配置文件
     */

    private static void initParameterSet() {
        new Thread(() -> {
            try {
                InputStream inputStream = new FileInputStream(new File(Environment.getExternalStorageDirectory()
                        + File.separator, "parameterSet.xml"));
//            InputStream inputStream = new FileInputStream(new File("/sdcard/", "parameterSet.xml"));

                PullParser.parseParamSet(inputStream);
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (Exception ex) {
                LatteLogger.d(TAG, "initparameterset exception...." + ex.getMessage());
                ex.printStackTrace();
            }
        }).start();

    }

    public static ParameterSet parameterSet;
    private static InputStream inputStream;
    private static BufferedReader bf;

    /**
     * 读取配置文件
     */
    private static void initParameterSetJson() {

        new Thread(() -> {
            try {
                inputStream = new FileInputStream(new File(Environment.getExternalStorageDirectory()
                        + File.separator, "parameter.json"));
//            InputStream inputStream = new FileInputStream(new File("/sdcard/", "parameterSet.xml"));
                StringBuilder builder = new StringBuilder();
                bf = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));
                String line;
                while ((line = bf.readLine()) != null) {
                    builder.append(line);
                }
                parameterSet = ParseJsonParameterSet.getParameterSet(builder.toString().replace(" ", ""));
                if (parameterSet != null) {
                    saveRidAndSendHeartbeatMs();
                }
                if (bf != null) {
                    bf.close();
                }
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (Exception ex) {
                LatteLogger.d(TAG, "initparameterset exception...." + ex.getMessage());
                ex.printStackTrace();
            }
        }).start();
    }

    /**
     * 保存配置文件
     */
    private static void setParameterSet() {
    }


    public static void initAppFunction(Context context) {
        startHotFixServer(context);
        startDelLogServer(context);
        initParameterSetJson();
    }

    private static Intent delLogSer;
    private static Intent hotFixSer;
    private static Intent heartBeatSer;

    public static void stopServiceMul() {
        if (delLogSer != null) {
            MyApplication.getApplicationMyContext().stopService(delLogSer);
        }
        if (hotFixSer != null) {
            MyApplication.getApplicationMyContext().stopService(hotFixSer);
        }
        if (heartBeatSer != null) {
            MyApplication.getApplicationMyContext().stopService(heartBeatSer);
        }
    }

    private static void startDelLogServer(Context context) {
        delLogSer = new Intent(context, AtuoDeleteLogService.class);
        context.startService(delLogSer);
    }

    private static void startHotFixServer(Context context) {
        hotFixSer = new Intent(context, AutoUpdateService.class);
        context.startService(hotFixSer);
    }


    private static void saveRidAndSendHeartbeatMs() {
        heartBeatSer = new Intent(LogUtil.context, HeartBeatService.class);
        LogUtil.context.startService(heartBeatSer);
    }
}
